/**
 * Created by qingzhu on 15/7/1.
 */
module qr {
    export class QRCode {
        /**
         * msg
         * width,height 二维码宽度，高度
         * color 颜色
         * 
         * errorCorrectLevel:
         * level L : 最大 7% 的错误能够被纠正；
         * level M : 最大 15% 的错误能够被纠正；
         * level Q : 最大 25% 的错误能够被纠正；
         * level H : 最大 30% 的错误能够被纠正；
         * 
         * typeNumber:
         * QR图的大小(size)被定义为版本（Version)，版本号从1到40。版本1就是一个21*21的矩阵，每增加一个版本号，矩阵的大小就增加4个模块(Module)，
         * 因此，版本40就是一个177*177的矩阵。（版本越高，意味着存储的内容越多，纠错能力也越强）。
         * */
        public static create(msg: string, width: number = 200, height: number = 200, errorCorrectLevel = QRErrorCorrectLevel.M, typeNumer: number = 4, color = 0): egret.Sprite {
            typeNumer = this.getLength(msg, typeNumer, errorCorrectLevel);
            console.log("typeNumer length", msg, typeNumer)
            var _htOption = {
                color: color,
                width: width,
                height: height,
                correctLevel: errorCorrectLevel,
                typeNumer: typeNumer
            };
            var _oQRCode = new qr.QRCodeModel(_htOption.typeNumer, _htOption.correctLevel);
            _oQRCode.addData(msg);
            _oQRCode.make();
            return QRCode.draw(_oQRCode, _htOption);
        }
        public static getLength(msg, typeNumer, errorCorrectLevel) {
            let length = typeNumer;
            while (length < 20) {
                let bit = new QR8bitByte(msg);
                var rsBlocks = QRRSBlock.getRSBlocks(length, errorCorrectLevel), buffer = new QRBitBuffer;
                let datalist = [];
                datalist.push(bit);
                for (var i = 0; i < datalist.length; i++) {
                    var data = datalist[i];
                    buffer.put(data.mode, 4), buffer.put(data.getLength(), QRUtil.getLengthInBits(data.mode, length)), data.write(buffer);
                }
                var totalDataCount = 0;
                for (var i = 0; i < rsBlocks.length; i++) totalDataCount += rsBlocks[i].dataCount;
                if (buffer.getLengthInBits() > totalDataCount * 8) {
                    length++;
                } else {
                    return length;
                }
            }

            return length;
        }

        public static draw(m: qr.QRCodeModel, _htOption): egret.Sprite {
            //最外层容器
            var container: egret.Sprite = new egret.Sprite();

            // 画一个灰色遮罩
            var shade: egret.Shape = new egret.Shape();
            shade.touchEnabled = true;
            shade.graphics.beginFill(0x030303);
            shade.graphics.drawRect(0, 0, Config.VR, Config.VB);
            shade.graphics.endFill();
            shade.alpha = 0.9;
            container.addChild(shade);

            //二维码容器
            var sc: egret.Sprite = new egret.Sprite();
            var _htOption = _htOption;
            var nCount = m.getModuleCount();
            var nWidth = (_htOption.width / nCount);
            var nHeight = (_htOption.height / nCount);
            container.addChild(sc);

            // 画一个比二维码本身略大的白色底框
            var borderWidth = 10;
            sc.graphics.moveTo(-borderWidth, -borderWidth);
            sc.graphics.beginFill(0xffffff);
            sc.graphics.drawRect(-borderWidth, -borderWidth, _htOption.width + 2 * borderWidth, _htOption.height + 2 * borderWidth);
            sc.graphics.endFill();
            // 画二维码
            for (var row = 0; row < nCount; row++) {
                for (var col = 0; col < nCount; col++) {
                    var b = m.isDark(row, col);
                    if (b) {
                        sc.graphics.moveTo(col * nWidth, row * nHeight);

                        sc.graphics.beginFill(_htOption.color);
                        sc.graphics.drawRect(col * nWidth, row * nHeight, nWidth, nHeight);
                        sc.graphics.endFill();
                    }
                }
            }
            sc.x = (Config.VR - sc.width) / 2 + borderWidth;
            sc.y = 150;

            // 微信图标
            var icon: egret.Bitmap = new egret.Bitmap(RES.getRes("wePayLogo_png"));
            container.addChild(icon);
            icon.x = (Config.VR - icon.width) / 2;
            icon.y = 30;

            // 微信提示文字
            var txt: egret.Bitmap = new egret.Bitmap(RES.getRes("wechat_payTxt_png"));
            container.addChild(txt);
            txt.x = (Config.VR - txt.width) / 2;
            txt.y = 500;

            //关闭按钮
            var close: egret.Bitmap = new egret.Bitmap(RES.getRes("common_close_btn_png"));
            container.addChild(close);
            // close.width = 80;
            // close.height = 82;
            close.x = 800;
            close.y = 40;
            close.touchEnabled = true;
            close.addEventListener(egret.TouchEvent.TOUCH_TAP, () => {
                container.parent.removeChild(container);
                container = null;
            }, this);


            return container;
        }

    }
}